home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / src / setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  14.8 KB  |  541 lines

  1. /* Setup loading/saving.
  2.    Copyright (C) 1994 Miguel de Icaza
  3.    
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <config.h>
  19. #include <sys/types.h>        /* Needed to include local .h files */
  20. #include <sys/stat.h>
  21. #include <sys/param.h>
  22. #include <string.h>
  23. #include "tty.h"
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include "mad.h"
  27. #include "dir.h"
  28. #include "file.h"
  29. #include "global.h"
  30. #include "util.h"        /* Functions and externs */
  31. #include "panel.h"
  32. #include "main.h"
  33. #include "tree.h"
  34. #include "profile.h"
  35. #define WANT_DEFAULTS
  36. #include "setup.h"
  37. #include "mouse.h"        /* To make view.h happy */
  38. #include "view.h"        /* For the externs */
  39. #include "key.h"        /* For the externs */
  40. #include "hotlist.h"        /* load/save/done hotlist */
  41. #include "panelize.h"        /* load/save/done panelize */
  42. #include "layout.h"
  43. #include "menu.h"        /* menubar_visible declaration */
  44. #include "win.h"        /* lookup_key */
  45. #include "../vfs/vfs.h"
  46. #ifdef USE_NETCODE
  47. #   include "../vfs/ftpfs.h"
  48. extern int use_netrc;
  49. extern int ftpfs_retry_seconds;
  50. #endif
  51.  
  52. /* "$Id: setup.c,v 1.9 1995/02/21 19:07:13 miguel Exp $" */
  53.  
  54. #ifdef USE_VFS
  55. extern int vfs_timeout;
  56. extern int tar_gzipped_memlimit;
  57. #endif
  58.  
  59. extern char *find_ignore_dirs;
  60.  
  61. char *profile_name;
  62.  
  63. char setup_color_string [4096];
  64. char term_color_string [4096];
  65.  
  66. #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
  67. #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
  68. #define save_string WritePrivateProfileString
  69.  
  70. int startup_left_mode;
  71. int startup_right_mode;
  72.  
  73. /* Ugly hack to allow panel_save_setup to work as a place holder for */
  74. /* default panel values */
  75. int saving_setup;
  76.  
  77. static struct {
  78.     char *key;
  79.     sortfn *sort_type;
  80. } sort_names [] = {
  81.     { "name",      (sortfn *) sort_name },
  82.     { "extension", (sortfn *) sort_ext },
  83.     { "time",      (sortfn *) sort_time },
  84.     { "atime",     (sortfn *) sort_atime },
  85.     { "ctime",     (sortfn *) sort_ctime },
  86.     { "size",      (sortfn *) sort_size },
  87.     { "inode",     (sortfn *) sort_inode },
  88.     { "unsorted",  (sortfn *) unsorted },
  89.     { 0, 0 }
  90. };
  91.  
  92. static struct {
  93.     char *key;
  94.     int  list_type;
  95. } list_types [] = {
  96.     { "full",  list_full },
  97.     { "brief", list_brief },
  98.     { "long",  list_long },
  99.     { "user",  list_user },
  100.     { 0, 0 }
  101. };
  102.  
  103. static struct {
  104.     char *opt_name;
  105.     int  opt_type;
  106. } panel_types [] = {
  107.     { "listing",   view_listing },
  108.     { "quickview", view_quick   },
  109.     { "info",      view_info },
  110.     { "tree",      view_tree },
  111.     { 0, 0 }
  112. };
  113.     
  114. static struct {
  115.     char *opt_name;
  116.     int *opt_addr;
  117. } layout [] = {
  118.     { "horizontal_split", &horizontal_split },
  119.     { "equal_split", &equal_split },
  120.     { "first_panel_size", &first_panel_size },
  121.     { "menubar_visible", &menubar_visible },
  122.     { "command_prompt", &command_prompt },
  123.     { "keybar_visible", &keybar_visible },
  124.     { "message_visible", &message_visible },
  125.     { "xterm_hintbar", &xterm_hintbar },
  126.     { "output_lines", &output_lines },
  127.     { "show_mini_info", &show_mini_info },
  128.     { 0, 0 }
  129. };
  130.  
  131. extern int preserve_uidgid;
  132.  
  133. static struct {
  134.     char *opt_name;
  135.     int  *opt_addr;
  136. } options [] = {
  137.     { "show_backups", &show_backups },
  138.     { "show_dot_files", &show_dot_files },
  139.     { "verbose", &verbose },
  140.     { "mark_moves_down", &mark_moves_down },
  141.     { "pause_after_run", &pause_after_run },
  142.     { "shell_patterns", &easy_patterns },
  143.     { "auto_save_setup", &auto_save_setup },
  144.     { "align_extensions", &align_extensions },
  145.     { "auto_menu", &auto_menu },
  146.     { "use_internal_view", &use_internal_view },
  147.     { "clear_before_exec", &clear_before_exec },
  148.     { "mix_all_files", &mix_all_files },
  149.     { "fast_reload", &fast_reload },
  150.     { "fast_reload_msg_shown", &fast_reload_w },
  151.     { "confirm_delete", &confirm_delete },
  152.     { "confirm_overwrite", &confirm_overwrite },
  153.     { "confirm_exit", &confirm_exit },
  154.     { "mouse_repeat_rate", &mou_auto_repeat },
  155.     { "double_click_speed", &double_click_speed },
  156.     { "eight_bit_clean", &eight_bit_clean },
  157.     { "full_eight_bits", &full_eight_bits },
  158.     { "confirm_view_dir", &confirm_view_dir },
  159.     { "tree_navigation_flag", &tree_navigation_flag },
  160.     { "mouse_move_pages", &mouse_move_pages },
  161.     { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
  162.     { "fast_refresh", &fast_refresh },
  163.     { "navigate_with_arrows", &navigate_with_arrows },
  164.     { "advanced_chown", &advanced_chfns },
  165.     { "drop_menus", &drop_menus },
  166.     { "wrap_mode",  &wrap_mode},
  167.     { "old_esc_mode", &old_esc_mode },
  168.     { "nice_rotating_dash", &nice_rotating_dash },
  169.     { "cd_symlinks", &cd_symlinks },
  170.     { "show_all_if_ambiguous", &show_all_if_ambiguous },
  171.     { "have_fast_cpu", &have_fast_cpu },
  172.     { "iconify_on_exec", &iconify_on_exec },
  173.     { "torben_fj_mode", &torben_fj_mode },
  174.     { "use_file_to_guess_type", &use_file_to_check_type },
  175.     { "alternate_plus_minus", &alternate_plus_minus },
  176.     { "only_leading_plus_minus", &only_leading_plus_minus },
  177.     { "show_output_starts_shell", &output_starts_shell },
  178.     { "panel_scroll_pages", &panel_scroll_pages },
  179.     { "dive_into_subdirs", &dive_into_subdirs },
  180.     { "preserve_uidgid", &preserve_uidgid },
  181.     { "xtree_mode", &xtree_mode },
  182. #ifdef USE_VFS    
  183.     { "tar_gzipped_memlimit", &tar_gzipped_memlimit },
  184.     { "vfs_timeout", &vfs_timeout },
  185.     { "vfs_use_targz_memlimit", &vfs_use_limit },
  186. #ifdef USE_NETCODE
  187.     { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
  188.     { "use_netrc", &use_netrc },
  189.     { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
  190. #endif
  191. #endif    
  192.     { 0, 0 }
  193. };
  194.  
  195. void panel_save_setup (WPanel *panel, char *section)
  196. {
  197.     char buffer [6];
  198.     int  i;
  199.     
  200.     sprintf (buffer, "%d", panel->reverse);
  201.     save_string (section, "reverse", buffer, profile_name);
  202.     sprintf (buffer, "%d", panel->case_sensitive);
  203.     save_string (section, "case_sensitive", buffer, profile_name);
  204.     for (i = 0; sort_names [i].key; i++)
  205.     if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
  206.         save_string (section, "sort_order",
  207.                        sort_names [i].key, profile_name);
  208.         break;
  209.     }
  210.  
  211.     for (i = 0; list_types [i].key; i++)
  212.     if (list_types [i].list_type == panel->list_type){
  213.         save_string (section, "list_mode",
  214.                        list_types [i].key, profile_name);
  215.         break;
  216.     }
  217.  
  218.     save_string (section, "user_format",
  219.                    panel->user_format, profile_name);
  220.     save_string (section, "mini_status_format",
  221.                    panel->mini_status_format, profile_name);
  222.     sprintf (buffer, "%d", panel->user_mini_status);
  223.     save_string (section, "user_mini_status", buffer,
  224.                    profile_name);
  225. }
  226.  
  227. void save_layout (void)
  228. {
  229.     char *profile;
  230.     int  i;
  231.     char buffer [6];
  232.  
  233.     profile = copy_strings (home_dir, PATH_SEP_STR ".mc.ini", 0);
  234.  
  235.     /* Save integer options */
  236.     for (i = 0; layout [i].opt_name; i++){
  237.     sprintf (buffer, "%d", *layout [i].opt_addr);
  238.     save_string ("Layout", layout [i].opt_name, buffer, profile);
  239.     }
  240.  
  241.     free (profile);
  242. }
  243.  
  244. void save_configure (void)
  245. {
  246.     char *profile;
  247.     int  i;
  248.  
  249.     profile = copy_strings (home_dir, PATH_SEP_STR ".mc.ini", 0);
  250.  
  251.     /* Save integer options */
  252.     for (i = 0; options [i].opt_name; i++)
  253.     set_int (profile, options [i].opt_name, *options [i].opt_addr);
  254.  
  255.     free (profile);
  256. }
  257.  
  258. static void panel_save_type (char *section, int type)
  259. {
  260.     int i;
  261.     
  262.     for (i = 0; panel_types [i].opt_name; i++)
  263.     if (panel_types [i].opt_type == type){
  264.         save_string (section, "display", panel_types [i].opt_name,
  265.              profile_name);
  266.         break;
  267.     }
  268. }
  269.  
  270. void save_panel_types ()
  271. {
  272.     int type;
  273.  
  274.     type = get_display_type (0);
  275.     panel_save_type ("New Left Panel", type);
  276.     if (type == view_listing)
  277.     panel_save_setup (left_panel, left_panel->panel_name);
  278.     type = get_display_type (1);
  279.     panel_save_type ("New Right Panel", type);
  280.     if (type == view_listing)
  281.     panel_save_setup (right_panel, right_panel->panel_name);
  282. }
  283.  
  284. void save_setup (void)
  285. {
  286.     char *profile;
  287. #ifdef USE_VFS
  288. #ifdef USE_NETCODE
  289.     extern char *ftpfs_anonymous_passwd;
  290.     extern char *ftpfs_proxy_host;
  291. #endif
  292. #endif
  293.     saving_setup = 1;
  294.     profile = copy_strings (home_dir, PATH_SEP_STR ".mc.ini", 0);
  295.  
  296.     save_layout ();
  297.     save_configure ();
  298.     save_string ("Dirs", "other_dir",
  299.                    get_other_type () == view_listing
  300.                    ? opanel->cwd : ".", profile);
  301.     WritePrivateProfileString ("Dirs", "current_is_left",
  302.                    get_current_index () == 0 ? "1" : "0", profile);
  303.     save_hotlist ();
  304.     save_panelize ();
  305.     save_panel_types ();
  306. #ifdef USE_VFS
  307. #ifdef USE_NETCODE
  308.     WritePrivateProfileString ("Misc", "ftpfs_password",
  309.                    ftpfs_anonymous_passwd, profile);
  310.     if (ftpfs_proxy_host)
  311.     WritePrivateProfileString ("Misc", "ftp_proxy_host",
  312.                    ftpfs_proxy_host, profile);
  313. #endif
  314. #endif
  315.     free (profile);
  316.     saving_setup = 0;
  317. }
  318.  
  319. void panel_load_setup (WPanel *panel, char *section)
  320. {
  321.     int i;
  322.     char buffer [40];
  323.     
  324.     panel->reverse = load_int (section, "reverse", 0);
  325.     panel->case_sensitive = load_int (section, "case_sensitive", 1);
  326.     
  327.     /* Load sort order */
  328.     load_string (section, "sort_order", "name", buffer, sizeof (buffer));
  329.     panel->sort_type = (sortfn *) sort_name;
  330.     for (i = 0; sort_names [i].key; i++)
  331.     if (strcasecmp (sort_names [i].key, buffer) == 0){
  332.         panel->sort_type = sort_names [i].sort_type;
  333.         break;
  334.     }
  335.  
  336.     /* Load the listing mode */
  337.     load_string (section, "list_mode", "full", buffer, sizeof (buffer));
  338.     panel->list_type = list_full;
  339.     for (i = 0; list_types [i].key; i++)
  340.     if (strcasecmp (list_types [i].key, buffer) == 0){
  341.         panel->list_type = list_types [i].list_type;
  342.         break;
  343.     }
  344.     
  345.     /* User formats */
  346.     if (panel->user_format)
  347.     free (panel->user_format);
  348.     
  349.     panel->user_format = strdup (get_profile_string (section, "user_format",
  350.                              DEFAULT_USER_FORMAT,
  351.                              profile_name));
  352.     if (panel->mini_status_format)
  353.     free (panel->mini_status_format);
  354.     
  355.     panel->mini_status_format =
  356.     strdup (get_profile_string (section, "mini_status_format",
  357.                     DEFAULT_USER_FORMAT, profile_name));
  358.     panel->user_mini_status =
  359.     load_int (section, "user_mini_status", 0);
  360.  
  361. }
  362.  
  363. static void load_layout (char *profile_name)
  364. {
  365.     int i;
  366.     
  367.     for (i = 0; layout [i].opt_name; i++)
  368.     *layout [i].opt_addr =
  369.         load_int ("Layout", layout [i].opt_name,
  370.                   *layout [i].opt_addr);
  371. }
  372.  
  373. static int load_mode (char *section)
  374. {
  375.     char buffer [20];
  376.     int  i;
  377.     
  378.     int mode = view_listing;
  379.     
  380.     /* Load the display mode */
  381.     load_string (section, "display", "listing", buffer, sizeof (buffer));
  382.  
  383.     for (i = 0; panel_types [i].opt_name; i++)
  384.     if (strcasecmp (panel_types [i].opt_name, buffer) == 0){
  385.         mode = panel_types [i].opt_type;
  386.         break;
  387.     }
  388.  
  389.     return mode;
  390. }
  391.  
  392. char *do_load_string (char *s, char *ss, char *def)
  393. {
  394.     char *buffer = xmalloc (128, "dls");
  395.     char *p;
  396.     
  397.     load_string (s, ss, def, buffer, 128);
  398.  
  399.     p = strdup (buffer);
  400.     free (buffer);
  401.     return p;
  402. }
  403.  
  404. void load_setup (void)
  405. {
  406.     static char *buffer;
  407.     char   *profile;
  408.     int    i;
  409. #ifdef USE_NETCODE
  410.     extern char *ftpfs_proxy_host;
  411. #endif
  412.     buffer = copy_strings (home_dir, PATH_SEP_STR ".mc.ini", 0);
  413.     
  414.     if (exist_file (buffer)){
  415.     profile = buffer;
  416.     } else if (exist_file (LIBDIR "mc.ini")){
  417.     profile = strdup (LIBDIR "mc.ini");
  418.     free (buffer);
  419.     } else {
  420.     profile = buffer;
  421.     }
  422.     
  423.     profile_name = profile;
  424.  
  425.     /* Load integer boolean options */
  426.     for (i = 0; options [i].opt_name; i++)
  427.     *options [i].opt_addr =
  428.         get_int (profile, options [i].opt_name, *options [i].opt_addr);
  429.  
  430.     load_layout (profile);
  431.  
  432.     /* The directory hot list */
  433.     load_hotlist ();
  434.     load_panelize ();
  435.  
  436.     startup_left_mode = load_mode ("New Left Panel");
  437.     startup_right_mode = load_mode ("New Right Panel");
  438.  
  439.     /* At least one of the panels is a listing panel */
  440.     if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
  441.     startup_left_mode = view_listing;
  442.     
  443.     if (!other_dir){
  444.     buffer = (char*) malloc (MC_MAXPATHLEN);
  445.     load_string ("Dirs", "other_dir", ".", buffer,
  446.                  MC_MAXPATHLEN);
  447.     if (vfs_file_is_local (buffer))
  448.         other_dir = buffer;
  449.     else
  450.         free (buffer);
  451.     }
  452. #ifdef USE_NETCODE
  453.     ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
  454. #endif
  455.     boot_current_is_left =
  456.     GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
  457.     
  458.     load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
  459.          sizeof (setup_color_string));
  460.     if (setup_color_string [0])
  461.     find_ignore_dirs = copy_strings (":", setup_color_string, ":", 0);
  462.     
  463.     /* The default color and the terminal dependent color */
  464.     load_string ("Colors", "base_color", "", setup_color_string,
  465.                  sizeof (setup_color_string));
  466.     load_string ("Colors", getenv ("TERM"), "",
  467.                  term_color_string, sizeof (term_color_string));
  468.  
  469.     /* Remove the temporal entries */
  470.     profile_clean_section ("Temporal:New Left Panel", profile_name);
  471.     profile_clean_section ("Temporal:New Right Panel", profile_name);
  472. #ifdef USE_VFS
  473. #ifdef USE_NETCODE
  474.     ftpfs_init_passwd ();
  475. #endif
  476. #endif
  477. }
  478.  
  479. #ifdef USE_VFS
  480. #ifdef USE_NETCODE
  481. char *load_anon_passwd ()
  482. {
  483.     char buffer [255];
  484.  
  485.     load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
  486.     if (buffer [0])
  487.     return strdup (buffer);
  488.     else
  489.     return 0;
  490. }
  491. #endif
  492. #endif
  493.  
  494. void done_setup (void)
  495. {
  496.     free (profile_name);
  497.     done_hotlist ();
  498.     done_panelize ();
  499. }
  500.  
  501. void load_keys_from_section (char *terminal, char *profile_name)
  502. {
  503.     char *section_name;
  504.     void *profile_keys;
  505.     char *key, *value, *valcopy;
  506.     int  key_code;
  507.  
  508.     if (!terminal)
  509.     return;
  510.  
  511.     section_name = copy_strings ("terminal:", terminal, 0);
  512.     profile_keys = profile_init_iterator (section_name, profile_name);
  513.     if (!profile_keys){
  514.     free (section_name);
  515.     return;
  516.     }
  517.     
  518.     while (profile_keys){
  519.     profile_keys = profile_iterator_next (profile_keys, &key, &value);
  520.     key_code = lookup_key (key);
  521.     valcopy = convert_controls (value);
  522.     if (key_code)
  523.         define_sequence (key_code, valcopy, MCKEY_NOACTION);
  524.     free (valcopy);
  525.     }
  526.     free (section_name);
  527.     return;
  528. }
  529.  
  530. void load_key_defs (void)
  531. {
  532.     load_keys_from_section (getenv ("TERM"), profile_name);
  533.     load_keys_from_section ("general", profile_name);
  534.  
  535.     load_keys_from_section (getenv ("TERM"), LIBDIR "mc.lib");
  536.     load_keys_from_section ("general", LIBDIR "mc.lib");
  537.  
  538.     /* We don't want a huge database loaded in core */
  539.     free_profile_name (LIBDIR "mc.lib");
  540. }
  541.